<?php

/*
    Client class
    
        Author:         Alfredo Mungo
        License:        LGPL
        Copyright:      2012
*/

/*
  Client detection class.
  
  Constructors:
    N/A
  
  Methods:
    N/A
  
  Static methods:
    getDevice()
    getOS()
    getBrowser()
*/
class Client {

    private static $device = null, $os = null, $browser = null, $mobile = null;

    /*
      Return the client device.
      
      The return value is null if the device cannot be determined, otherwise it is a string containing one of the following:
        android
        iphone
        ipad
        windows
        blackberry
    */
    static function getDevice() {
        
        static $devices = array(
          array('android', 'android'),
          array('iphone', 'iphone'),
          array('ipad', 'ipad'),
          array('windows phone', 'windows'),
          array('blackberry', 'blackberry')
        );
        
        if(self::$device === null)
            self::$device = self::extractFromUA($devices);
        
        return self::$device;
    }
    
    /*
      Return the client operative system.
      
      The return value is null if the OS cannot be identified, otherwise it is a string containing one of the following:
        windows
        macosx
        linux
        qnx
    */
    static function getOS() {
    
        static $oss = array(
          array('windows', 'windows'),
          array('mac os x', 'macosx'),
          array('linux', 'linux'),
          array('qnx', 'qnx')
        );
        
        if(self::$os === null)
            self::$os = self::extractFromUA($oss);
            
        return self::$os;
    }
    
    /*
      Return the browser name of the client.
      
      The return value is null if the browser cannot be identified, otherwise it is a string containing one of the following:
        chrome
        firefox
        msie10
        msie9
        msie8
        msie7
        msie6
        opera
        safari
        iemobile (internet explorer mobile)
        lynx
        elinks
    */
    static function getBrowser() {
        // DO NOT CHANGE THE ELEMENTS ORDER!
        static $browsers = array(
          array('chrome', 'chrome'),
          array('firefox', 'firefox'),
          array('msie 10.', 'msie10'),
          array('msie 9.', 'msie9'),
          array('msie 8.', 'msie8'),
          array('msie 7.', 'msie7'),
          array('msie 6.', 'msie6'),
          array('opera', 'opera'),
          array('safari', 'safari'),
          array('webkit', 'chrome'),
          array('iemobile', 'iemobile'),
          array('mozilla', 'firefox'),
          array('gecko', 'firefox'),
          array('lynx', 'lynx'),
          array('elinks', 'elinks')
        );
        
        if(self::$browser === null)
            self::$browser = self::extractFromUA($browsers);
        
        return self::$browser;
    }
    
    /*
      Check an array of (ID, NAME) against the User Agent, returning NAME if ID is contained into the UA, null otherwise.
    */
    private static function extractFromUA($arr) {
        $ua = strtolower($_SERVER['HTTP_USER_AGENT']);
        
        foreach($arr as $data)
        {
            $id = $data[0];
            $name = $data[1];
            
            if(strpos($ua, $id) !== FALSE)
                return $name;
        }
    
        return null;
    }
    
    /*
        Check if the device is mobile.
        
        Return:
            TRUE if the device is mobile, FALSE if not.
    */
    public static function isMobile() {
    
        if(self::$mobile === null) {
            $ua = strtolower($_SERVER['HTTP_USER_AGENT']);
            $idx = strpos($ua, " mobile");
            self::$mobile = ($idx !== false);
        }
        
        return self::$mobile;
    }
    
}

?>